﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace PuzzleDiamonds
{
    public class SceneExplodeParticleEmitter : SceneObject
    {
        private class Particle : SceneSprite
        {
            private double m_totalLifeTime;
            private double m_lifeTime;
            private double m_lastTick;

            public double LifeTime
            {
                get { return m_lifeTime; }
            }

            public Particle(String pParticleImage, Point pPosition, Point pDirection, double pSpeed, double pLifeTime)
                : base(pParticleImage, pPosition, pDirection, pSpeed)
            {
                m_totalLifeTime = m_lifeTime = pLifeTime;
                m_lastTick = 0;
            }

            public override void Update(double pTick)
            {
                base.Update(pTick);

                if (m_lastTick == 0)
                    m_lastTick = pTick;
                else
                {
                    m_lifeTime -= (pTick - m_lastTick);
                    m_lastTick = pTick;

                    m_spriteImage.Opacity = (m_lifeTime / m_totalLifeTime);
                }
            }
        }

        private Random m_random;
        private Scene m_parentScene;        
        private Collection<Particle> m_particleList = new Collection<Particle>();
        private int m_nParticleCountPerFrame;
        private int m_nMaxParticleCount;
        private int m_nParticleEmitted;
        private double m_particleLifeTime;
        private double m_particleMaxSpeed;
        private String m_particleImage;
        private bool m_bActive;

        public bool Active
        {
            get { return m_bActive; }
        }

        public SceneExplodeParticleEmitter(String pParticleImage, Point pPosition, Point pDirection, double pSpeed, Scene pParentScene, int pParticleCountPerFrame, int pMaxParticleCount, double pParticleLifeTime, double pParticleMaxSpeed)
            : base(pPosition, pDirection, pSpeed)
        {
            m_parentScene = pParentScene;
            m_nParticleCountPerFrame = pParticleCountPerFrame;
            m_nMaxParticleCount = pMaxParticleCount;
            m_particleLifeTime = pParticleLifeTime;
            m_particleMaxSpeed = pParticleMaxSpeed;
            m_particleImage = pParticleImage;
            m_nParticleEmitted = 0;
            m_bActive = true;
            m_random = new Random();
        }

        public override Visual GetVisualObject()
        {
            return null;
        }

        public override void Update(double pTick)
        {
            base.Update(pTick);

            // update particle...
            int i = 0;
            while (i < m_particleList.Count)
            {
                m_particleList[i].Update(pTick);
                if (m_particleList[i].LifeTime <= 0.0)
                {
                    m_parentScene.RemoveObject(m_particleList[i]);
                    m_particleList.RemoveAt(i);
                    continue;
                }
                i++;                
            }

            // spawn particle if needed
            int nParticleToAdd = (m_nParticleCountPerFrame - m_particleList.Count);
            if (nParticleToAdd > 0)
            {
                if ((m_nMaxParticleCount == -1) || (m_nParticleEmitted < m_nMaxParticleCount))
                {
                    m_nParticleEmitted += nParticleToAdd;

                    for (int p = 0; p < nParticleToAdd; p++)
                    {
                        Point particleDirection = new Point(Math.Cos(m_random.NextDouble() * Math.PI * 2.0),
                                                            Math.Sin(m_random.NextDouble() * Math.PI * 2.0));
                        double particleSpeed = m_random.NextDouble() * m_particleMaxSpeed;
                        double particleLifeTime = /*m_random.NextDouble() */ m_particleLifeTime;

                        Particle particle = new Particle(m_particleImage, m_vPosition, particleDirection, particleSpeed, particleLifeTime);
                        m_parentScene.AddObject(particle);
                        m_particleList.Add(particle);
                    }
                }
                else
                    m_bActive = false;

            }
        }
    }
}
